/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lge.clock.worldclock.map;

import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.opengl.GLUtils;
import android.util.Log;
import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;
import javax.microedition.khronos.opengles.GL11Ext;
import com.lge.clock.AlarmClockActivity;

/**
 * An OpenGL text label maker.
 *
 *
 * OpenGL labels are implemented by creating a Bitmap, drawing all the labels
 * into the Bitmap, converting the Bitmap into an Alpha texture, and creating a
 * mesh for each label
 *
 * The benefits of this approach are that the labels are drawn using the high
 * quality anti-aliased font rasterizer, full character set support, and all the
 * text labels are stored on a single texture, which makes it faster to use.
 *
 * The drawbacks are that you can only have as many labels as will fit onto one
 * texture, and you have to recreate the whole texture if any label text
 * changes.
 * 
 * @author lge.com
 */

public class LabelMaker {
    private static final int LABEL_POSITION_MODE_RIGHT_TOP = 1;
    private static final int LABEL_POSITION_MODE_RIGHT_BOTTOM = 2;
    private static final int LABEL_POSITION_MODE_LEFT_BOTTOM = 3;
    private static final int LABEL_POSITION_MODE_LEFT_TOP = 4;
	private static final int SELECT_MARGIN_Y = 10;
    
    private Rect mLableTextPadding;
    
    /**
     * Create a label maker
     * or maximum compatibility with various OpenGL ES implementations,
     * the strike width and height must be powers of two,
     * We want the strike width to be at least as wide as the widest window.
     *
     * @param fullColor true if we want a full color backing store (4444),
     * otherwise we generate a grey L8 backing store.
     * @param strikeWidth width of strike
     * @param strikeHeight height of strike
     */
    public LabelMaker(boolean fullColor, int strikeWidth, int strikeHeight) {
        mFullColor = fullColor;
        mStrikeWidth = strikeWidth;
        mStrikeHeight = strikeHeight;
        mTexelWidth = (float) (1.0 / mStrikeWidth);
        mTexelHeight = (float) (1.0 / mStrikeHeight);
        mClearPaint = new Paint();
        mClearPaint.setARGB(0, 0, 0, 0);
        mClearPaint.setStyle(Style.FILL);
        mState = STATE_NEW;
    }

    /**
     * Call to initialize the class.
     * Call whenever the surface has been created.
     *
     * @param gl
     */
    public void initialize(GL10 gl) {
        mState = STATE_INITIALIZED;
        int[] textures = new int[1];
        gl.glGenTextures(1, textures, 0);
        mTextureID = textures[0];
        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);

        // Use Nearest for performance.
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
                GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
                GL10.GL_NEAREST);

        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
                GL10.GL_CLAMP_TO_EDGE);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
                GL10.GL_CLAMP_TO_EDGE);

        gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,
                GL10.GL_REPLACE);
    }

    /**
     * Call when the surface has been destroyed
     */
    public void shutdown(GL10 gl) {
        if ( gl != null) {
            if (mState > STATE_NEW) {
                int[] textures = new int[1];
                textures[0] = mTextureID;
                gl.glDeleteTextures(1, textures, 0);
                mState = STATE_NEW;
            }
        }
    }

    /**
     * Call before adding labels. Clears out any existing labels.
     *
     * @param gl
     */
    public void beginAdding(GL10 gl) {
        checkState(STATE_INITIALIZED, STATE_ADDING);
        mLabels.clear();
        mU = 0;
        mV = 0;
        mLineHeight = 0;
        Bitmap.Config config = mFullColor ?
                Bitmap.Config.ARGB_4444 : Bitmap.Config.ALPHA_8;
        mBitmap = Bitmap.createBitmap(mStrikeWidth, mStrikeHeight, config);
        mCanvas = new Canvas(mBitmap);
        mBitmap.eraseColor(0);
    }

    /**
     * Call to add a label
     *
     * @param gl
     * @param text the text of the label
     * @param textPaint the paint of the label
     * @return the id of the label, used to measure and draw the label
     */
    public int add(GL10 gl, String text, Paint textPaint) {
        return add(gl, null, text, textPaint);
    }
    
    /**
     * add bitmap image.
     */
    public int add(GL10 gl, String text, Paint textPaint, Bitmap image, int position) {
        return add(gl, null, text, textPaint, image, position);
    }

    /**
     * Call to add a label
     *
     * @param gl
     * @param text the text of the label
     * @param textPaint the paint of the label
     * @return the id of the label, used to measure and draw the label
     */
    public int add(GL10 gl, Drawable background, String text, Paint textPaint) {
        return add(gl, background, text, textPaint, 0, 0);
    }
    
    /**
     * add bitmap image.
     */
    public int add(GL10 gl, Drawable background, String text, Paint textPaint, Bitmap image, int position) {
        return add(gl, background, text, textPaint, 0, 0, image, position);
    }

    /**
     * Call to add a label
     * @return the id of the label, used to measure and draw the label
     */
    public int add(GL10 gl, Drawable drawable, int minWidth, int minHeight) {
        return add(gl, drawable, null, null, minWidth, minHeight);
    }

    /**
     * Call to add a label
     *
     * @param gl
     * @param text the text of the label
     * @param textPaint the paint of the label
     * @return the id of the label, used to measure and draw the label
     */
    public int add(GL10 gl, Drawable background, String text, Paint textPaint,
            int minWidth, int minHeight) {
        checkState(STATE_ADDING, STATE_ADDING);
        boolean drawBackground = background != null;
        boolean drawText = (text != null) && (textPaint != null);

        Rect padding = new Rect();
        if (drawBackground) {
            background.getPadding(padding);
            minWidth = Math.max(minWidth, background.getMinimumWidth());
            minHeight = Math.max(minHeight, background.getMinimumHeight());
        }

        int ascent = 0;
        int descent = 0;
        int measuredTextWidth = 0;
        if (drawText) {
            // Paint.ascent is negative, so negate it.
            ascent = (int) Math.ceil(-textPaint.ascent());
            descent = (int) Math.ceil(textPaint.descent());
            measuredTextWidth = (int) Math.ceil(textPaint.measureText(text));
        }
        int textHeight = ascent + descent;
        int textWidth = Math.min(mStrikeWidth,measuredTextWidth);

        int padHeight = padding.top + padding.bottom;
        int padWidth = padding.left + padding.right;
        int height = Math.max(minHeight, textHeight + padHeight);
        int width = Math.max(minWidth, textWidth + padWidth);
        int effectiveTextHeight = height - padHeight;
        int effectiveTextWidth = width - padWidth;

        int centerOffsetHeight = (effectiveTextHeight - textHeight) / 2;
        int centerOffsetWidth = (effectiveTextWidth - textWidth) / 2;

        // Make changes to the local variables, only commit them
        // to the member variables after we've decided not to throw
        // any exceptions.

        int u = mU;
        int v = mV;
        int lineHeight = mLineHeight;

        if (width > mStrikeWidth) {
            width = mStrikeWidth;
        }

        // Is there room for this string on the current line?
        if (u + width > mStrikeWidth) {
            // No room, go to the next line:
            u = 0;
            v += lineHeight;
            lineHeight = 0;
        }
        lineHeight = Math.max(lineHeight, height);
        if (v + lineHeight > mStrikeHeight) {
            IllegalArgumentException iaExcep = new IllegalArgumentException("Out of texture space.");
    		throw iaExcep;
        }

        int u2 = u + width;
        int vBase = v + ascent;
        int v2 = v + height;

        if (drawBackground) {
            background.setBounds(u, v, u + width, v + height);
            background.draw(mCanvas);
        }

        if (drawText) {
            mCanvas.drawText(text,
                    u + padding.left + centerOffsetWidth,
                    vBase + padding.top + centerOffsetHeight,
                    textPaint);
        }

        Grid grid = new Grid(2, 2);
        // Grid.set arguments: i, j, x, y, z, u, v

        float texU = u * mTexelWidth;
        float texU2 = u2 * mTexelWidth;
        float texV = 1.0f - v * mTexelHeight;
        float texV2 = 1.0f - v2 * mTexelHeight;

        grid.set(0, 0,   0.0f,   0.0f, 0.0f, texU , texV2);
        grid.set(1, 0,  width,   0.0f, 0.0f, texU2, texV2);
        grid.set(0, 1,   0.0f, height, 0.0f, texU , texV );
        grid.set(1, 1,  width, height, 0.0f, texU2, texV );

        // We know there's enough space, so update the member variables
        mU = u + width;
        mV = v;
        mLineHeight = lineHeight;
        mLabels.add(new Label(grid, width, height, ascent,
                u, v + height, width, -height));
        return mLabels.size() - 1;
    }
    
    /**
     * add bitmap image.
     */
    public int add(GL10 gl, Drawable background, String text, Paint textPaint,
            int minWidth, int minHeight, Bitmap image, int position) {
        checkState(STATE_ADDING, STATE_ADDING);
        boolean drawBackground = background != null;
        boolean drawText = (text != null) && (textPaint != null);

        Rect padding = new Rect();
        if (drawBackground) {
            background.getPadding(padding);
            minWidth = Math.max(minWidth, background.getMinimumWidth());
            minHeight = Math.max(minHeight, background.getMinimumHeight());
        }

        int ascent = 0;
        int descent = 0;
        int measuredTextWidth = 0;
        if (drawText) {
            // Paint.ascent is negative, so negate it.
            ascent = (int) Math.ceil(-textPaint.ascent());
            descent = (int) Math.ceil(textPaint.descent());
            measuredTextWidth = (int) Math.ceil(textPaint.measureText(text));
        }
        int textHeight = ascent + descent;
        int textWidth = Math.min(mStrikeWidth,measuredTextWidth);
        
        int textAndImageHeight = textHeight;
        int textAndImageWidth = textWidth;

        try {
        	textAndImageHeight = textHeight + image.getHeight();
        	textAndImageWidth = textWidth + image.getWidth();
        } catch (Exception e) {
        	e.printStackTrace();
        }

        int padHeight = padding.top + padding.bottom;
        int padWidth = padding.left + padding.right;
        int height = Math.max(minHeight, textAndImageHeight + padHeight);
        int width = Math.max(minWidth, textAndImageWidth + padWidth);
        int effectiveTextHeight = height - padHeight;
        int effectiveTextWidth = width - padWidth;
        
        int centerOffsetHeight = (effectiveTextHeight - textAndImageHeight) / 2;
        int centerOffsetWidth = (effectiveTextWidth - textAndImageWidth) / 2;

        // Make changes to the local variables, only commit them
        // to the member variables after we've decided not to throw
        // any exceptions.

        int u = mU;
        int v = mV;
        int lineHeight = mLineHeight;

        if (width > mStrikeWidth) {
            width = mStrikeWidth;
        }

        // Is there room for this string on the current line?
        if (u + width > mStrikeWidth) {
            // No room, go to the next line:
            u = 0;
            v += lineHeight;
            lineHeight = 0;
        }
        lineHeight = Math.max(lineHeight, height);
        if (v + lineHeight > mStrikeHeight) {
            IllegalArgumentException iaExcep = new IllegalArgumentException("Out of texture space.");
    		throw iaExcep;
        }

        int u2 = u + width;
        int vBase = v + ascent;
        int v2 = v + height;

        int imagePaddingTop = 0;
        int imagePaddingLeft = 0;
        int textPaddingTop = 0;
        
        switch (position) {
            case LABEL_POSITION_MODE_RIGHT_TOP:
                imagePaddingTop = textHeight + padHeight;
                imagePaddingLeft = padding.left;
                textPaddingTop = 0;
                break;
            case LABEL_POSITION_MODE_RIGHT_BOTTOM:
                imagePaddingTop = 0;
                imagePaddingLeft = padding.left;
                textPaddingTop = image.getHeight();//Math.min(image.getHeight(), height-minHeight);
                break;
            case LABEL_POSITION_MODE_LEFT_BOTTOM:
                imagePaddingTop = 0;
                imagePaddingLeft = width - image.getWidth() - padding.right;
                textPaddingTop = image.getHeight();//Math.min(image.getHeight(), height-minHeight);
                break;
            case LABEL_POSITION_MODE_LEFT_TOP:
                imagePaddingTop = textHeight + padHeight;
                imagePaddingLeft = width - image.getWidth() - padding.right;
                textPaddingTop = 0;
                break;
            default :
        }
        
        if (drawBackground) {
            background.setBounds(u, 
                                 v + textPaddingTop,
                                 u + width,
                                 v + textPaddingTop + Math.max(minHeight, textHeight + padHeight));
            background.draw(mCanvas);
        }

        if (drawText) {
        	if(!drawBackground) {
        		textPaint.setMaskFilter(new BlurMaskFilter(1, Blur.NORMAL));
            	textPaint.setColor(0xFF000000);         	
            	textPaint.setFakeBoldText(true);
                mCanvas.drawText(text,
                        u + padding.left + centerOffsetWidth,
                        vBase + padding.top + centerOffsetHeight + textPaddingTop,
                        textPaint);
                textPaint.setMaskFilter(null);
                textPaint.setColor(0xFFFFFFFF);             
                textPaint.setFakeBoldText(false);
        	}
            mCanvas.drawText(text,
                    u + padding.left + centerOffsetWidth,
                    vBase + padding.top + centerOffsetHeight + textPaddingTop,
                    textPaint);
            mCanvas.drawBitmap(image,
                    u + imagePaddingLeft,
                    v + imagePaddingTop,
                    textPaint);
        }
        mLableTextPadding = padding;
        Grid grid = new Grid(2, 2);
        // Grid.set arguments: i, j, x, y, z, u, v

        float texU = u * mTexelWidth;
        float texU2 = u2 * mTexelWidth;
        float texV = 1.0f - v * mTexelHeight;
        float texV2 = 1.0f - v2 * mTexelHeight;

        grid.set(0, 0,   0.0f,   0.0f, 0.0f, texU , texV2);
        grid.set(1, 0,  width,   0.0f, 0.0f, texU2, texV2);
        grid.set(0, 1,   0.0f, height, 0.0f, texU , texV );
        grid.set(1, 1,  width, height, 0.0f, texU2, texV );

        // We know there's enough space, so update the member variables
        mU = u + width;
        mV = v;
        mLineHeight = lineHeight;
        mLabels.add(new Label(grid, width, height, image.getWidth(), image.getHeight(), 
                ascent, u, v + height, width, -height, position));
        return mLabels.size() - 1;
    }

    /**
     * Call to end adding labels. Must be called before drawing starts.
     *
     * @param gl
     */
    public void endAdding(GL10 gl) {
        checkState(STATE_ADDING, STATE_INITIALIZED);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, mBitmap, 0);
        // Reclaim storage used by bitmap and canvas.
        mBitmap.recycle();
        mBitmap = null;
        mCanvas = null;
    }

    /**
     * Get the width in pixels of a given label.
     *
     * @param labelID
     * @return the width in pixels
     */
    public float getWidth(int labelID) {
        return mLabels.get(labelID).width;
    }
    
    public int getImageWidth(int labelID) {
        return mLabels.get(labelID).imageWidth;
    }

    /**
     * Get the height in pixels of a given label.
     *
     * @param labelID
     * @return the height in pixels
     */
    public float getHeight(int labelID) {
        return mLabels.get(labelID).height;
    }
    
    public int getImageHeight(int labelID) {
        return mLabels.get(labelID).imageHeight;
    }

    /**
     * Get the baseline of a given label. That's how many pixels from the top of
     * the label to the text baseline. (This is equivalent to the negative of
     * the label's paint's ascent.)
     *
     * @param labelID
     * @return the baseline in pixels.
     */
    public float getBaseline(int labelID) {
        return mLabels.get(labelID).baseline;
    }

    public int getPosition(int labelID) {
    	return mLabels.get(labelID).position;
    }
    
	public int getTextStartX(float sx, int labelId) {
        Label label = mLabels.get(labelId);
        float widthPosition = 0;
        
        switch (label.position) {
            case LABEL_POSITION_MODE_RIGHT_TOP:
            case LABEL_POSITION_MODE_RIGHT_BOTTOM:
                widthPosition = 0;
                break;
            case LABEL_POSITION_MODE_LEFT_BOTTOM:
            case LABEL_POSITION_MODE_LEFT_TOP:
                widthPosition = -(label.width);
                break;
            default :
        }
        
        return (int)(sx + widthPosition);
	}
    
	public int getTextStartY(float sy, int labelId) {
        Label label = mLabels.get(labelId);
        float heightPosition = 0;

        switch (label.position) {
            case LABEL_POSITION_MODE_RIGHT_TOP:
            case LABEL_POSITION_MODE_LEFT_TOP:
                heightPosition = -(label.imageHeight);
                break;
            case LABEL_POSITION_MODE_RIGHT_BOTTOM:
            case LABEL_POSITION_MODE_LEFT_BOTTOM:
                heightPosition = (label.imageHeight);
                break;
            default :
        }

        return (int)(sy + heightPosition)-SELECT_MARGIN_Y;
	}

	public int getTextEndX(float sx, int labelId) {
        Label label = mLabels.get(labelId);
        return (int)(sx + label.width);
	}
	
	public int getTextEndY(float sy, int labelId) {
        Label label = mLabels.get(labelId);
        return (int)(sy + (label.height - label.imageHeight))+SELECT_MARGIN_Y;
	}
        
    /**
     * Begin drawing labels. Sets the OpenGL state for rapid drawing.
     *
     * @param gl
     * @param viewWidth
     * @param viewHeight
     */
    public void beginDrawing(GL10 gl, float viewWidth, float viewHeight) {
        checkState(STATE_INITIALIZED, STATE_DRAWING);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID);
        gl.glShadeModel(GL10.GL_FLAT);
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        gl.glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glPushMatrix();
        gl.glLoadIdentity();
        gl.glOrthof(0.0f, viewWidth, 0.0f, viewHeight, 0.0f, 1.0f);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glPushMatrix();
        gl.glLoadIdentity();
        // Magic offsets to promote consistent rasterization.
        gl.glTranslatef(0.375f, 0.375f, 0.0f);
    }

    /**
     * Draw a given label at a given x,y position, expressed in pixels, with the
     * lower-left-hand-corner of the view being (0,0).
     *
     * @param gl
     * @param x
     * @param y
     * @param labelID
     */
    public void draw(GL10 gl, float x, float y, int labelID) {
        checkState(STATE_DRAWING, STATE_DRAWING);
        gl.glPushMatrix();
        gl.glLoadIdentity();
        
        Label label = mLabels.get(labelID);
        float widthPosition = 0;
        float heightPosition = 0;
        
        switch (label.position) {
            case LABEL_POSITION_MODE_RIGHT_TOP:
                widthPosition = -(label.imageWidth/2+mLableTextPadding.left);
                heightPosition = 0;
                break;
            case LABEL_POSITION_MODE_RIGHT_BOTTOM:
                widthPosition = -(label.imageWidth/2+mLableTextPadding.left);
                heightPosition = -(label.height - label.imageHeight);
                break;
            case LABEL_POSITION_MODE_LEFT_BOTTOM:
                widthPosition = -(label.width - (label.imageWidth/2+mLableTextPadding.right));  //-(label.width - (label.imageWidth * 2));
                heightPosition = -(label.height - label.imageHeight);
                break;
            case LABEL_POSITION_MODE_LEFT_TOP:
                widthPosition = -(label.width - (label.imageWidth/2+mLableTextPadding.right));  //-(label.width - (label.imageWidth * 2));
                heightPosition = 0;
                break;
            default :
        }
        
        x = x + widthPosition;
        y = y + heightPosition;
        
        float snappedX = (float) Math.floor(x);
        float snappedY = (float) Math.floor(y);
        gl.glTranslatef(snappedX, snappedY, -10.0f);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        ((GL11)gl).glTexParameteriv(GL10.GL_TEXTURE_2D,
                GL11Ext.GL_TEXTURE_CROP_RECT_OES, label.mCrop, 0);
        ((GL11Ext)gl).glDrawTexiOES((int) snappedX, (int) snappedY, 0,
                (int) label.width, (int) label.height);
        gl.glPopMatrix();
    }
    
    /**
     * Ends the drawing and restores the OpenGL state.
     *
     * @param gl
     */
    public void endDrawing(GL10 gl) {
        checkState(STATE_DRAWING, STATE_INITIALIZED);
        gl.glDisable(GL10.GL_BLEND);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glPopMatrix();
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glPopMatrix();
    }

    private void checkState(int oldState, int newState) {
        if (mState != oldState) {
            IllegalArgumentException iaExcep = new IllegalArgumentException("Can't call this method now.");
    		throw iaExcep;
        }
        mState = newState;
    }
    
    public void clear() {
        if (mLabels != null) {
            mLabels.clear();
            mLabels = null;
        }
        
        if (mBitmap != null) {
            mBitmap.recycle();
            mBitmap = null;
        }
    }

    /**
     * @author lge.com
     */
    public static class Label {
        public Label(Grid grid, float width, float height, float baseLine,
                int cropU, int cropV, int cropW, int cropH) {
            this.grid = grid;
            this.width = width;
            this.height = height;
            this.baseline = baseLine;
            int[] crop = new int[4];
            crop[0] = cropU;
            crop[1] = cropV;
            crop[2] = cropW;
            crop[3] = cropH;
            mCrop = crop;
        }
        
        public Label(Grid grid, float width, float height,
                int imageWidth, int imageHeight, float baseLine,
                int cropU, int cropV, int cropW, int cropH, int position) {
            this.grid = grid;
            this.width = width;
            this.height = height;
            this.imageWidth = imageWidth;
            this.imageHeight = imageHeight;
            this.baseline = baseLine;
            int[] crop = new int[4];
            crop[0] = cropU;
            crop[1] = cropV;
            crop[2] = cropW;
            crop[3] = cropH;
            mCrop = crop;
            this.position = position;
        }

        public Grid grid;
        public float width;
        public float height;
        public int imageWidth;
        public int imageHeight;
        public int position;
        public float baseline;
        public int[] mCrop;
    }

    private int mStrikeWidth;
    private int mStrikeHeight;
    private boolean mFullColor;
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Paint mClearPaint;

    private int mTextureID;

    private float mTexelWidth;  // Convert texel to U
    private float mTexelHeight; // Convert texel to V
    private int mU;
    private int mV;
    private int mLineHeight;
    private ArrayList<Label> mLabels = new ArrayList<Label>();

    private static final int STATE_NEW = 0;
    private static final int STATE_INITIALIZED = 1;
    private static final int STATE_ADDING = 2;
    private static final int STATE_DRAWING = 3;
    private int mState;

}
